#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/12/6 17:51
# @Author  : lizhen
# @Site    : 
# @File    : sort.py
# @Software: PyCharm
import random
from collections import deque

import numpy


def bucket_sort(nums):
    max_len = max(nums)
    buckets = [0] * (max_len + 1)
    for i in nums:
        buckets[i] += 1
    sorted_nums = []
    for index, num in enumerate(buckets):
        if num != 0:
            for i in range(num):
                sorted_nums.append(index)
    print(sorted_nums)


def bubble_sort(l):
    length = len(l)
    for index in range(length - 1, 0, -1):
        flag = 0
        for two_index in range(index):
            if l[two_index] > l[two_index + 1]:
                l[two_index], l[two_index + 1] = l[two_index + 1], l[two_index]
                flag = 1
        if flag == 0:
            break
    print(l)


def quick_sort(li, start, end):
    if start > end:
        return
    low = start
    key = li[low]
    high = end
    while low < high:
        while low < high and li[high] >= key:
            high -= 1
        li[low] = li[high]
        while low < high and li[low] <= key:
            low += 1
        li[high] = li[low]
    li[low] = key
    quick_sort(li, start, low - 1)
    quick_sort(li, low + 1, end)


def fibo(num):
    numList = [0, 1]
    for i in range(num - 2):
        numList.append(numList[-2] + numList[-1])
    print(numList)


def remove_max_value(nums):
    new_nums = [item for item in nums if item != numpy.max(nums)]
    print(new_nums)


def insert_ordered_list():
    nums = [0, 10, 20, 30]
    value = int(input('请输入要插入的数值：'))
    nums.append(value)
    length = len(nums)
    for index, item in enumerate(nums):
        if value < item:
            for j in range(length - 1, index, -1):
                nums[j] = nums[j - 1]
            nums[index] = value
            break
    print(nums)


def eval_value():
    nums = [lambda x: i * x for i in range(4)]
    # nums = [lambda x: 0, lambda x: x, lambda x: 2 * x, lambda x: 3 * x]
    print([m(2) for m in nums])


def qsort(nums):
    if len(nums) < 2:
        return nums
    split_value = random.choice(nums)
    lefts = [item for item in nums if item < split_value]
    rights = [item for item in nums if item > split_value]
    return qsort(lefts) + [split_value] + qsort(rights)


class ListReverse:
    def __init__(self, num):
        self.num = num

    def splice_reverse(self):
        print(self.num[::-1])

    def splice_link(self):
        if len(self.num) < 1:
            return self.num
        last = self.num[0]
        self.num = self.num[1:]
        return self.splice_link() + last

    def swap(self):
        nums = list(self.num)
        length = len(nums)
        for i, j in zip(range(length - 1, 0, -1), range(length // 2)):
            nums[i], nums[j] = nums[j], nums[i]
        print(''.join(nums))

    def deque_extendleft(self):
        d = deque()
        d.extendleft(self.num)
        print(''.join(d))

    def range_reverse(self):
        print(''.join([self.num[item] for item in range(len(self.num) - 1, -1, -1)]))


def main():
    # fibo(7)
    # a = [2, 4, 5, 7, 7, 1]
    # bucket_sort(a)
    # bubble_sort(a)
    # quick_sort(a, 0, len(a) - 1)
    # print(a)
    # remove_max_value(a)
    # insert_ordered_list()
    # eval_value()
    # print(qsort([2, 3, 8, 4, 9, 5, 6] + [5, 6, 10, 17, 11, 2]))
    lr = ListReverse('abcdef')
    # lr.splice_reverse()
    # print(lr.splice_link())
    # lr.swap()
    # lr.deque_extendleft()
    lr.range_reverse()


if __name__ == '__main__':
    main()
